<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2009 – Consider using `pgrep` instead of grepping `ps` output.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2009 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2009">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2 id="consider-using-pgrep-instead-of-grepping-ps-output">Consider
using <code>pgrep</code> instead of grepping <code>ps</code>
output.</h2>
<h3 id="problematic-code">Problematic Code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2009.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ps</span> ax <span class="kw">|</span> <span class="fu">grep</span> <span class="at">-v</span> grep <span class="kw">|</span> <span class="fu">grep</span> <span class="st">&quot;</span><span class="va">$service</span><span class="st">&quot;</span> <span class="op">&gt;</span> /dev/null</span></code></pre></div>
<h3 id="correct-code">Correct Code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2009.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ex">pgrep</span> <span class="at">-f</span> <span class="st">&quot;</span><span class="va">$service</span><span class="st">&quot;</span> <span class="op">&gt;</span> /dev/null</span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>If you are just after a pid from a running program, then pgrep is a
much safer alternative. Especially if you are also looking for a pid
belonging to a certain user or group. All of the parameters are in one
command and it can eliminate multiple greps, cuts, seds, awks, etc.</p>
<p>If you want a field that's not the pid, consider doing this through
<code>ps</code> + <code>pgrep</code> instead of <code>ps</code> +
<code>grep</code>:</p>
<pre><code>for pid in $(pgrep &#39;^python$&#39;)
do
  user=$(ps -o user= -p &quot;$pid&quot;)
  echo &quot;The process $pid is run by $user&quot;
done</code></pre>
<p>This is more robust than <code>ps .. | grep python | cut ..</code>
because it does not try to match against unrelated fields, such as if
the user's name was <code>pythonguru</code>.</p>
<h3 id="exceptions">Exceptions</h3>
<p><code>pgrep</code> is not POSIX. Please <a href="ignore.html">ignore</a>
this warning if you are targeting POSIX userlands.</p>
<p>You can <a href="ignore.html">ignore</a> this error if you are trying to
match against something that <code>pgrep</code> doesn't support:</p>
<pre><code># pgrep does not support filtering by &#39;nice&#39; value
# shellcheck disable=SC2009
ps -axo nice=,pid= | grep -v &#39;^  0&#39;</code></pre>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


